package com.xiaoluyy.demo.common.util;

import java.util.List;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TTransportFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xiaoluyy.common.rpc.service.thrift.ServiceProxy;
import com.xiaoluyy.common.rpc.service.thrift.ThriftService;

public class ThriftRpcService {

    private final static Logger logger = LoggerFactory
            .getLogger(ThriftRpcService.class);
    
    private String thriftServiceConfFile;
    private ThriftService thriftService;
    private String thriftServiceName;
    private List<ThriftRpcServiceConfig> serviceConfigList;
    private TTransportFactory transportFactory;
    private TProtocolFactory protocolFactory;
    private String serverType;
    
    private ThriftRpcService(String thriftServiceConfFile)
    {
    	this.thriftServiceConfFile = thriftServiceConfFile;
    }
    
    public ThriftRpcService()
    {
    	transportFactory = new TTransportFactory();
    	protocolFactory = new TBinaryProtocol.Factory();
    	serverType = "threadselector";
    }
    
    public ThriftRpcService(TTransportFactory transportFactory,TProtocolFactory protocolFactory,String serverType)
    {
    	this.transportFactory = transportFactory;
    	this.protocolFactory = protocolFactory;
    	this.serverType = serverType;
    }
    

    public void startup() {
        try {
            this.thriftService = new ThriftService(transportFactory,protocolFactory,serverType);
            for (ThriftRpcServiceConfig config : this.serviceConfigList) {
                ServiceProxy serviceProxy = new ServiceProxy(Class.forName(config.getServiceClazz()),
                		config.getServiceImpl(),config.getRegisteFlag());
                
                this.thriftService.add(serviceProxy);
            }

            Thread serviceThread = new Thread(new ThriftServiceThread(this.thriftService));
            serviceThread.start();

            logger.debug("thrift service " + this.thriftServiceName
                    + " startup success.");
        } catch (Exception e) {
            logger.error("thrift service " + this.thriftServiceName
                    + " startup error.", e);
        }

    }

    public void shutdown() {
        if (this.thriftService != null) {
            try {
                this.thriftService.stop();

                logger.debug("thrift service " + this.thriftServiceName
                        + " shutdown success.");
            } catch (Exception e) {
                logger.error("thrift service " + this.thriftServiceName
                        + " shutdown error.", e);
            }
        }
    }

    public void setThriftServiceName(String thriftServiceName) {
		this.thriftServiceName = thriftServiceName;
	}

    public void setServiceConfigList(
            List<ThriftRpcServiceConfig> serviceConfigList) {
        this.serviceConfigList = serviceConfigList;
    }

	public TTransportFactory getTransportFactory() {
		return transportFactory;
	}

	public void setTransportFactory(TTransportFactory transportFactory) {
		this.transportFactory = transportFactory;
	}

	public TProtocolFactory getProtocolFactory() {
		return protocolFactory;
	}

	public void setProtocolFactory(TProtocolFactory protocolFactory) {
		this.protocolFactory = protocolFactory;
	}

	public String getServerType() {
		return serverType;
	}

	public void setServerType(String serverType) {
		this.serverType = serverType;
	}
	
	static class ThriftServiceThread implements Runnable
	{
		private ThriftService thriftService;
		public ThriftServiceThread(ThriftService thriftService)
		{
			this.thriftService = thriftService;
		}

		public void run() {
			thriftService.init();
		}
		
	}
    

}
